/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDOptions.java
 *
 * Created on April 2, 2006, 11:11 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd;

import java.io.*;
import ssi_socket.*;

/**
 *
 * @author John
 */
public class PTDOptions {
    
    public static String m_DefaultWorkspace;
    //public static String m_DefaultAgent;
    //public static String m_DefaultAgentPort;
    //public static String m_DefaultMachine;
    public static String m_LicenseKey;
    public static String m_DefaultDirectory;
    
    private static String m_EvalTimer = "";
    private static final long m_MaxEvalDays = 30;
    
    static String TAG_DEFAULT_WORKSPACE = "PTD_DEFAULT_WORKSPACE";
    //static String TAG_DEFAULT_AGENT = "PTD_DEFAULT_AGENT";
    //static String TAG_DEFAULT_AGENT_PORT = "PTD_DEFAULT_AGENT_PORT";
    //static String TAG_DEFAULT_MACHINE = "PTD_DEFAULT_MACHINE";
    static String TAG_LICENSE_KEY = "PTD_LICENSE_KEY";
    static String TAG_EVAL_TIMER = "PTD_DATA_FLAGS";
    static String TAG_DEFAULT_DIRECTORY = "PTD_DEFAULT_DIRECTORY";
    
    
    
    
    /** Creates a new instance of PTDOptions */
    public PTDOptions() {
    }
    
    public static void init(){
        // Consider replacing with 'Properties' class... sets up as XML (see Java book)
        //m_DefaultAgent = Main.getAppDir() + "/PhantomAgent.exe";
        //m_DefaultWorkspace = Main.getAppDir() + "/Default.sui";
        m_DefaultWorkspace = "";
        m_DefaultDirectory = "";
        //m_DefaultAgentPort = "3000";
        m_LicenseKey = "";
        m_EvalTimer = "";
        //m_DefaultMachine = SSI_Socket.getHostName(); 
    }
    
    public static int loadFromFile(String sFile){
        File afile = new File(sFile);
        String sOpts = "";
        String sLine = "";
        String LineSep = System.getProperty("line.separator");
        String OptSep = "=";
        String sTag = "";
        String sValue = "";
        int i;
        int j;
        if(afile == null){
            return 0;
        }
        
        if(afile.isFile() == false){
            return 0;
        }

        // Read in the file
        try{
            char c = 0;
            FileReader aReader = new FileReader(afile);
            while((c = (char)aReader.read()) < 255){
                sOpts = sOpts + c;
            }            
        }catch (Exception e){
            Main.debug_out(e.getMessage());
        }
        
        while((i = sOpts.indexOf(LineSep)) >= 0){
            if(i > 0){
                sLine = sOpts.substring(0, i);
                if((j = sLine.indexOf(OptSep)) > 0){
                    sTag = sLine.substring(0, j);
                    sValue = sLine.substring(j+1);
                    setOption(sTag, sValue);
                }
                sOpts = sOpts.substring(i+LineSep.length());
            }else{
                sOpts = sOpts.substring(i+LineSep.length());
            }
        }
        
        sLine = sOpts;
        if((j = sLine.indexOf(OptSep)) > 0){
            sTag = sLine.substring(0, j);
            sValue = sLine.substring(j+1);
            setOption(sTag, sValue);
        }
        
        return 1;
    }

    public static int saveToFile(String sFile){
        File afile = new File(sFile);
//        String sOpts = "";
//        String sLine = "";
        String OptSep = "=";
//        String sTag = "";
//        String sValue = "";
//        int i;
//        int j;
        if(afile == null){
            return 0;
        }
        
        if(afile.isFile() == false){
            return 0;
        }

        try{            
            BufferedWriter aWriter = new BufferedWriter(new FileWriter(afile));            
            aWriter.write(TAG_LICENSE_KEY);
            aWriter.write(OptSep);
            aWriter.write(m_LicenseKey);
            aWriter.newLine();
            
            aWriter.write(TAG_DEFAULT_WORKSPACE);
            aWriter.write(OptSep);
            aWriter.write(m_DefaultWorkspace);
            aWriter.newLine();

            aWriter.write(TAG_DEFAULT_DIRECTORY);
            aWriter.write(OptSep);
            aWriter.write(m_DefaultDirectory);
            aWriter.newLine();

            aWriter.write(TAG_EVAL_TIMER);
            aWriter.write(OptSep);
            aWriter.write(m_EvalTimer);
            aWriter.newLine();
            
            //aWriter.write(TAG_DEFAULT_AGENT);
            //aWriter.write(OptSep);
            //aWriter.write(m_DefaultAgent);
            //aWriter.newLine();

            //aWriter.write(TAG_DEFAULT_AGENT_PORT);
            //aWriter.write(OptSep);
            //aWriter.write(m_DefaultAgentPort);
            //aWriter.newLine();

            //aWriter.write(TAG_DEFAULT_MACHINE);
            //aWriter.write(OptSep);
            //aWriter.write(m_DefaultMachine);
            //aWriter.newLine();
            aWriter.close();
            
        }catch (Exception e){
            Main.debug_out(e.getMessage());
        }
        
        return 1;
    }
    
    public static int setOption(String sTag, String sValue){
        sTag = sTag.trim();
        sValue = sValue.trim();
        if(sTag.compareTo(TAG_DEFAULT_WORKSPACE) == 0){
            m_DefaultWorkspace = sValue;
            return 1;
        }else if(sTag.compareTo(TAG_DEFAULT_DIRECTORY) == 0){
            m_DefaultDirectory = sValue;
            return 1;            
//        }else if(sTag.compareTo(TAG_DEFAULT_AGENT) == 0){
//            m_DefaultAgent = sValue;
//            return 1;
//        }else if(sTag.compareTo(TAG_DEFAULT_AGENT_PORT) == 0){
//            m_DefaultAgentPort = sValue;
//            return 1;
        }else if(sTag.compareTo(TAG_LICENSE_KEY) == 0){
            m_LicenseKey = sValue;
            return 1;
        }else if(sTag.compareTo(TAG_EVAL_TIMER) == 0){
            m_EvalTimer = sValue;
            return 1;
            
//        }else if(sTag.compareTo(TAG_DEFAULT_MACHINE) == 0){
//            m_DefaultMachine = sValue;
//            return 1;
        }
        return 0;
    }
    
    public static String getFilePath(String sIn){
      String bSlash = "\\";
      String fSlash = "/";
      String Ret;
      int bi;
      int fi;
      int i;
      
      bi = sIn.lastIndexOf(bSlash);
      fi = sIn.lastIndexOf(fSlash);
      if(bi > fi){
          i = bi;
      }else{
          i = fi;
      }
      
      if(i >= 0){
          Ret = sIn.substring(0, i);
      }else{
          Ret = sIn;
      }

      return Ret;        
    }
    
    public static String getFileFile(String sIn){
      String bSlash = "\\";
      String fSlash = "/";
      String Ret;
      int bi;
      int fi;
      int i;
      
      bi = sIn.lastIndexOf(bSlash);
      fi = sIn.lastIndexOf(fSlash);
      if(bi > fi){
          i = bi;
      }else{
          i = fi;
      }
      
      if(i >= 0){
          Ret = sIn.substring(i+1);
      }else{
          Ret = sIn;
      }

      return Ret;
    }
    
    public static boolean checkEvalTimer(){
        if(m_EvalTimer.length() <= 0){
            m_EvalTimer = makeEvalTimer();
            saveToFile(getOptionsFile());
            return false;
        }
        
        String CurrTimer = makeEvalTimer();
        long daydiff = calcDayDiff(m_EvalTimer, CurrTimer);
        
        if(daydiff < 0){
            return true;  // expired
        }
        
        if(daydiff > m_MaxEvalDays){
            return true; // expired
        }
        
        return false;
    }
    
    private static String makeEvalTimer(){
        long iMills = System.currentTimeMillis();
        iMills = iMills / 2;
        iMills = iMills + 640*86400*1000; // Add 640 days
        return Long.toString(iMills, 22);
    }
    
    private static long calcMillis(String s){
        long iMills = Long.parseLong(s, 22);
        iMills = iMills - 640*86400*1000;
        iMills = iMills*2;
        return iMills;
    }
    
    private static long calcDayDiff(String s1, String s2){
        long i1 = calcMillis(s1);
        long i2 = calcMillis(s2);
        return (i2 - i1)/(86400*1000);
    }
    
    public static String getOptionsFile(){
        String sOptsFile = Main.getAppDir() + "/ptd_options.dat";
        return sOptsFile;
        
    }
    
}


